package com.example.shushiyunsst.fragment.main.wallet.sub

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.fastjson.JSON
import com.example.library_common.adapter.multi22.JssBaseMultiAdapter
import com.example.library_common.base.MultiListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.utils.even.MessageWrap
import com.example.library_common.utils.even.TO_REFRESH_BALANCE_INFO
import com.example.library_common.utils.even.TO_REFRESH_BALANCE_INFO_Failed
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.getUserBalanceInfo
import com.example.shushiyunsst.fragment.main.wallet.bean.MyCaptureBean
import com.example.shushiyunsst.fragment.main.wallet.bean.MyCaptureCListBean
import com.example.shushiyunsst.fragment.main.wallet.bean.MyCaptureInterfaces
import com.example.shushiyunsst.fragment.main.wallet.bean.MyCaptureListBean
import com.example.shushiyunsst.fragment.main.wallet.item_provider.MyCaptureCListProvider
import com.example.shushiyunsst.fragment.main.wallet.item_provider.MyCaptureListProvider
import com.example.wallet.db.BalanceInfoManager
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

class MyNewCaptureFragment : MultiListFragment<MyCaptureInterfaces>(), WalletLabInfo {

    companion object {
        fun newInstance(): MyNewCaptureFragment {
            val args = Bundle()
            val fragment = MyNewCaptureFragment()
            fragment.arguments = args
            return fragment
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        delayedTime = 0
        mSuperBackPressedSupport = false
        isAddDividerItemDecoration = false
        isEnableLoadMore = false
    }

    override fun initView(view: View) {
        super.initView(view)
        mSwipeRefreshLayout?.setColorSchemeResources(
            R.color.nav_bar_bg_color,
            R.color.bg_color_4,
            R.color.bg_color_10
        )
        mRecyclerView?.overScrollMode = RecyclerView.OVER_SCROLL_NEVER
        mSwipeRefreshLayout?.setProgressBackgroundColorSchemeResource(R.color.colorAccent)
    }

    override fun addOnListItemProvider(mAdapter: JssBaseMultiAdapter<MyCaptureInterfaces?>?) {
        mAdapter?.addItemProvider(MyCaptureCListProvider())
        mAdapter?.addItemProvider(MyCaptureListProvider())
    }

    override fun netRequest() {
        getUserBalanceInfo(_mActivity)
    }

    override val listType: Type = TypeBuilder.newInstance(CommonBean::class.java)
        .beginSubType(List::class.java)
        .addTypeParam(MyCaptureInterfaces::class.java)
        .endSubType()
        .build()


    override val title: Int = R.string.my_capture

    override fun refreshData() {
        if (isLoaded) {
            delayedTime = 1000
            if (mSwipeRefreshLayout?.isRefreshing == false) {
                mSwipeRefreshLayout?.isRefreshing = true
                onRefresh()
            }

        }
    }

    override fun handMessage(msg: MessageWrap?) {
        super.handMessage(msg)
        when (msg?.message) {
            TO_REFRESH_BALANCE_INFO -> {
                loadData()
            }
            TO_REFRESH_BALANCE_INFO_Failed -> {
                onFailed()
            }
        }
//        if (msg?.message == TO_REFRESH_BALANCE_INFO) {
//
//        }
    }

    private fun loadData() {
        val balanceInfoBean = BalanceInfoManager.mBalanceInfoManager.getBalanceInfoBean()
        val ls = mutableListOf<MyCaptureInterfaces>()

        val listStr = JSON.toJSONString(balanceInfoBean?.list)
        ls.addAll(JSON.parseArray(listStr, MyCaptureListBean::class.java))

        val cListStr = JSON.toJSONString(balanceInfoBean?.getcList())
        ls.addAll(JSON.parseArray(cListStr, MyCaptureCListBean::class.java))
        loadListDate(ls)

//        headerListAdapter?.clears()
//        headerListAdapter?.data?.addAll(balanceInfoBean?.getcList()?: emptyList())

        delayedTime = 0
    }
}